<cfcomponent displayname="ReactorAdapter" extends="actionpacks.persistence.PersistenceService" output="false">

	<cffunction name="init" returntype="any" output="false" hint="Constructor">
		<cfargument name="ormObjectName" required="true">
		<cfargument name="SVNORMService" required="true">
		<cfset variables.instance.ormobjectname = arguments.ormObjectName>
		<cfset variables.instance.SVNORMService = arguments.SVNORMService />
		<cfreturn this />
	</cffunction>

	<cffunction name="setSVNORMService" access="public" returntype="any" output="false" roles="" hint="Import: ORM Service">
		<cfargument name="SVNORMService"  required="true" type="any" />
		<cfset variables.instance.SVNORMService = arguments.SVNORMService />
	</cffunction>

	<cffunction name="getSVNORMService" access="private" returntype="any" output="false" roles="" hint="Returns Internal: ORM Service">
		<cfreturn variables.instance.SVNORMService />
	</cffunction>

	<cffunction name="getGateway" access="public" output="false" returntype="any">
		<cfargument name="gatewayobject"  default="#variables.instance.ormobjectname#" type="any" />
		<cfscript>
			var gateway = getSVNORMService().createGateway(arguments.gatewayobject);			
			return gateway;
		</cfscript>
	</cffunction>

	<cffunction name="getRecord" access="public" output="false" returntype="any">
		<cfargument name="recordobjectname"  default="#variables.instance.ormobjectname#" type="any" />
		<cfscript>
			var record = getSVNORMService().createRecord(arguments.recordobjectname);			
			return record;
		</cfscript>
	</cffunction>

	<cffunction name="persist" returntype="any" access="public" output="false">
		<cfargument name="persistObject" type="any" required="true" hint="object to persist"/>
		<cfargument name="useTransaction" type="boolean" default="false" />
		<cfscript>
			var recordOb = getRecord();
			var popRec = recordOb.populate(persistObject);
			var record = recordOb.load();
			var errors = "";
			var dict = "";
			var errorCollection = createObject("component", "ModelGlue.Util.ValidationErrorCollection").init();
			var x = 0;
			record.validate();

			if(NOT record.hasErrors()) {
				record.save(arguments.useTransaction);
			} 
			else {
				errors = record._getErrorCollection().getErrors();
				dict = record._getDictionary();
				for(x=1; x lte arrayLen(errors); x = x+1) {
					errorCollection.addError(listGetAt(errors[x], 2, "."), dict.getValue(errors[x]));
				}
			}
//			dumpvar(record._getTo());
			return errorCollection.getErrors();
		</cfscript>
	</cffunction>


	<cffunction name="persisted" returntype="any" output="false" hint="Create Project Object">
		<cfargument name="persistedObject" type="any" required="true" hint="object to persist"/>
		<cfargument name="useTransaction" type="boolean" default="false" />
		<cfscript>
			var record = getRecord();
			record.populate(persistedObject);
			record.load();
			populate(arguments.persistedObject,record);
			return persistedObject;
		</cfscript>
	</cffunction>


</cfcomponent>